var consts = require('../../../util/consts');
var userSync = require('../../../dao/mapping/userSync');

module.exports = function (app) { return new Handler(app); }
var Handler = function (app) { this.app = app; this.consts = consts; };
var handler = Handler.prototype;

/**
 * 获取用户排行榜
 * @param msg
 * @param session
 * @param next
 */
handler.c2sRankList = function (msg, session, next) {
    var self = this;
    var uid = session.get('uid');
    var redis = self.app.get('redis');
    let worldRankKey = consts.RedisPrefix.WORLD_RANK_PREFIX;
    let guardianKey = consts.RedisPrefix.WORLD_RANK_GUARDIAN_PREFIX;
    self.app.rpc.login.loginRemote.getUser(session, uid, function (err, userInfo) {
        if (!userInfo) {
            next(null, {
                code: self.consts.Code.ERR,
                msg: self.consts.Msg.USER_NOT_FOUND
            });
            return;
        }
        redis.exists(worldRankKey, function (err, data) {
            if (data != 0) {
                console.log("////===>>>>>>>>>有排行榜缓存");
                redis.get(worldRankKey, function (err, data) {
                    let resData = JSON.parse(data);
                    let rank = 100;
                    for (let i = 0; i < resData.length; i++) {
                        if(resData[i].uid == userInfo.uid){
                            rank = i;
                            break;
                        }
                    }
                    next(null, {
                        code: self.consts.Code.SUC,
                        msg: self.consts.Msg.RESULT_SUCCESS,
                        data: {
                            rank_list: resData,
                            my_rank: {
                                rank: rank + 1,
                                title: userInfo.title,
                                oil_total: userInfo.total_oil,
                                deep_level: userInfo.depth_level,
                                nickname: userInfo.nickname,
                                avatar: userInfo.avatar,
                                contact_info: userInfo.contact_info || '',    //用户联系方式
                            }
                        }
                    });
                    return;
                })
            } else { //获取失败，需要从mongodb中同步最新数据
                //查询排行榜数据
                console.log("////===>>>>>>>>>没有排行榜缓存，查数据库");
                userSync.oilRank(function (err, data) {
                    if (err) {
                        next(null, {
                            code: consts.Code.ERR,
                            msg: err
                        })
                        return;
                    }
                    let resData = [];
                    var rank = 200;
                    for (var i = 0; i < data.length; i++) {
                        var isMe = data[i].uid === uid ? true : false;
                        if (isMe) {
                            rank = i;
                        }
                        resData.push({
                            title: data[i].title,
                            nickname: data[i].nickname,
                            avatar: data[i].avatar ? data[i].avatar : '',
                            is_me: isMe,
                            oil_total: data[i].total_oil,
                            deep_level: data[i].depth_level,
                            rank: i + 1,
                            uid:data[i].uid,
                            contact_info: data[i].contact_info || '',    //用户联系方式
                        });
                    }

                    next(null, {
                        code: self.consts.Code.SUC,
                        msg: self.consts.Msg.RESULT_SUCCESS,
                        data: {
                            rank_list: resData,
                            my_rank: {
                                rank: rank + 1,
                                title: userInfo.title,
                                oil_total: userInfo.total_oil,
                                deep_level: userInfo.depth_level,
                                nickname: userInfo.nickname,
                                avatar: userInfo.avatar,
                                contact_info: userInfo.contact_info || '',    //用户联系方式
                            }
                        }
                    });

                    //把数据放入守护层内
                    redis.set(worldRankKey, JSON.stringify(resData), function (err, data) {
                        redis.expire(worldRankKey, 300, function (err, data) {
                        });
                    });
                });
            }
        })

    });
}
